﻿using System;
using System.Deployment.Application;
using System.Diagnostics;
using System.Reflection;
using System.Windows;
using System.Windows.Input;
using SABnzbdNET.Commands;

namespace SABnzbdNET.ViewModels
{
    public class AboutViewModel : BaseViewModel
    {
        #region Fields and properties

        private DelegateCommand closeWindowCommand;
        private DelegateCommand updateApplicationCommand;
        private DelegateCommand launchBrowserCommand;

        private string updateAvailableText;
        private bool updateAvailable;
        private string appVersion;
        private string appName;
        private string appDescription;
        private string appCopyright;

        public ICommand CloseWindowCommand
        {
            get { return closeWindowCommand ?? (closeWindowCommand = new DelegateCommand(CloseWindow)); }
        }
        public ICommand UpdateApplicationCommand
        {
            get { return updateApplicationCommand ?? (updateApplicationCommand = new DelegateCommand(UpdateApplication)); }
        }
        public ICommand LaunchBrowserCommand
        {
            get { return launchBrowserCommand ?? (launchBrowserCommand = new DelegateCommand(LaunchBrowser)); }
        }

        public string UpdateAvailableText
        {
            get { return updateAvailableText; }
            set
            {
                updateAvailableText = value;
                OnPropertyChanged("UpdateAvailableText");
            }
        }
        public bool UpdateAvailable
        {
            get { return updateAvailable; }
            set
            {
                updateAvailable = value;
                OnPropertyChanged("UpdateAvailable");
            }
        }
        public string AppVersion
        {
            get { return appVersion; }
            set
            {
                appVersion = value;
                OnPropertyChanged("AppVersion");
            }
        }
        public string AppName
        {
            get { return appName; }
            set
            {
                appName = value;
                OnPropertyChanged("AppName");
            }
        }
        public string AppDescription
        {
            get { return appDescription; }
            set
            {
                appDescription = value;
                OnPropertyChanged("AppDescription");
            }
        }
        public string AppCopyright
        {
            get { return appCopyright; }
            set
            {
                appCopyright = value;
                OnPropertyChanged("AppCopyright");
            }
        }

        #endregion

        public AboutViewModel()
        {
            GetAssemblyInfo();
            GetUpdateInfo();
        }

        private static void CloseWindow()
        {
            Application.Current.Windows[1].Close();
        }

        private void GetAssemblyInfo()
        {
            Assembly assem = Assembly.GetExecutingAssembly();
            AssemblyName assemName = assem.GetName();
            AppName = assemName.Name;

            if (ApplicationDeployment.IsNetworkDeployed)
                AppVersion = ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();
            else
                AppVersion = assemName.Version.ToString();

            object[] attributes = assem.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
            if (attributes.Length != 0)
                AppDescription = ((AssemblyDescriptionAttribute)attributes[0]).Description;

            attributes = assem.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
            if (attributes.Length != 0)
                AppCopyright = ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
        }

        private void GetUpdateInfo()
        {
            UpdateAvailable = false;
            UpdateAvailableText = "No updates available.";

            UpdateCheckInfo info;

            if (ApplicationDeployment.IsNetworkDeployed)
            {
                ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;

                try
                {
                    info = ad.CheckForDetailedUpdate();
                }
                catch (DeploymentDownloadException)
                {
                    UpdateAvailableText = "The new version of the application cannot be downloaded at this time.";
                    return;
                }
                catch (InvalidDeploymentException)
                {
                    UpdateAvailableText = "Cannot check for a new version of the application.";
                    return;
                }
                catch (InvalidOperationException)
                {
                    UpdateAvailableText = "This application cannot be updated.";
                    return;
                }

                if (info.UpdateAvailable)
                {
                    UpdateAvailable = true;
                    UpdateAvailableText = "An update is available.";
                }
            }
        }

        private void UpdateApplication()
        {
            if (ApplicationDeployment.IsNetworkDeployed)
            {
                try
                {
                ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
                ad.Update();
                System.Windows.Forms.Application.Restart();
                Application.Current.Shutdown();
                }
                catch (DeploymentDownloadException)
                {
                    UpdateAvailableText = "Cannot install the latest version of the application. \n\nPlease check your network connection, or try again later.";
                    return;
                }
            }
        }

        private static void LaunchBrowser()
        {
            Process.Start(new ProcessStartInfo("http://sabnzbdnet.codeplex.com"));
        }
    }
}
